package controller

import (
	"errors"
	"ordersystem/config"
	"ordersystem/model"
	"ordersystem/util"

	"github.com/gofiber/fiber/v2"
)

func DeviceQueryDeviceController(ctx *fiber.Ctx) error {
	var device model.Device
	ctx.BodyParser(&device)
	if er := device.QueryDevice(); er != nil {
		return er
	}
	util.SendResponse(ctx, device)
	return nil
}

type DevicesQueryParams struct {
	Page  int `json:"page" xml:"page" form:"page"`
	LabId int `json:"lab_id" xml:"lab_id" form:"lab_id"`
}

func DeviceQueryDevicesController(ctx *fiber.Ctx) error {
	var DevicesQueryParams DevicesQueryParams
	if err := ctx.BodyParser(&DevicesQueryParams); err != nil {
		return errors.New(config.REQUEST_DATAFORMATION_ERROR)
	}

	results, err := model.QueryDevicesByLabId(DevicesQueryParams.Page - 1)
	if err != nil {
		return err
	}
	util.SendResponse(ctx, results)
	return nil
}

func DeviceUpdateDeviceController(ctx *fiber.Ctx) error {
	var device model.Device
	ctx.BodyParser(&device)

	if ctx.Cookies("identify") == "0" {
		if err := device.UpdateDevice(); err != nil {
			return err
		}
		util.SendResponse(ctx, nil)
		return nil
	}

	if ctx.Cookies("identify") == "2" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	var originDevice model.Device
	originDevice.Id = device.Id

	if err := originDevice.QueryDevice(); err != nil {
		return err
	}

	cookie := util.GetCookieForMap(ctx)

	if originDevice.Creator != cookie["id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := device.UpdateDevice(); err != nil {
		return err
	}
	util.SendResponse(ctx, nil)

	return nil
}

func DeviceDeleteDeviceController(ctx *fiber.Ctx) error {
	var device model.Device
	ctx.BodyParser(&device)

	if ctx.Cookies("identify") == "0" {
		if err := device.DeleteDevice(); err != nil {
			return err
		}
		util.SendResponse(ctx, nil)
		return nil
	}

	if ctx.Cookies("identify") == "2" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	var originDevice model.Device
	originDevice.Id = device.Id

	if err := originDevice.QueryDevice(); err != nil {
		return err
	}

	cookie := util.GetCookieForMap(ctx)

	if device.Creator != cookie["id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := device.DeleteDevice(); err != nil {
		return err
	}
	util.SendResponse(ctx, nil)

	return nil
}

func DeviceCreateDeviceController(ctx *fiber.Ctx) error {
	var device model.Device
	ctx.BodyParser(&device)
	cookie := util.GetCookieForMap(ctx)
	device.Creator = cookie["id"].(int32)

	if cookie["identify"] == int32(2) {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	var lab model.Lab

	lab.Id = device.LabId
	if err := lab.QueryLab(); err != nil {
		return err
	}

	if lab.Creator != cookie["id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := device.CreateDevice(); err != nil {
		return err
	}

	util.SendResponse(ctx, device)
	return nil
}

type LabId struct {
	LabId int `json:"lab_id" xml:"lab_id" form:"lab_id"`
}

func DeviceQueryNumberController(ctx *fiber.Ctx) error {

	var labId LabId
	if err := ctx.BodyParser(&labId); err != nil {
		return err
	}

	if number, err := model.QueryDeviceNumber(labId.LabId); err != nil {
		return err
	} else {
		util.SendResponse(ctx, number)
		return nil
	}
}
